/**
 * AES对称加密
 */
const CryptoJS = require('crypto-js')

/**
 * aes加密不带偏移量参数
 * @param word
 * @param aesKey
 * @returns {string}
 */
function encrypt(word, aesKey) {
  let key = CryptoJS.enc.Utf8.parse(aesKey)
  let srcs = CryptoJS.enc.Utf8.parse(word)
  let encrypyed = CryptoJS.AES.encrypt(srcs, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })
  return encrypyed.toString()
}

/**
 * 解密 不带偏移量参数
 * @param word
 * @param aesKey
 * @returns {*}
 */
function decrypt(word, aesKey) {
  let key = CryptoJS.enc.Utf8.parse(aesKey);
  let decrypt = CryptoJS.AES.decrypt(word, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7});
  return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}


let content = "贾克杰森";
let key = "junsonjackcn"; //key 里面有特殊字符就会解密不出来
console.log("前端加密前：" + content);
console.log("前端加密密钥和解密密钥：" + key);
let encryptStr = encrypt(content, key);
console.log("前端加密后：" + encryptStr);
let decryptStr = decrypt(encryptStr, key);
console.log("前端解密后：" + decryptStr);

let encryptStrServer = "JTqfgOVgj8YCIT+VVWCNCg==";
console.log("后端原始密文：" + encryptStrServer);
let decryptStrServer = decrypt(encryptStrServer, key);
console.log("后端密文解密后：" + decryptStrServer);

/*前端加密前：贾克杰森
前端加密密钥和解密密钥：junsonjackcn
前端加密后：JTqfgOVgj8YCIT+VVWCNCg==
前端解密后：贾克杰森
后端原始密文：JTqfgOVgj8YCIT+VVWCNCg==
后端密文解密后：贾克杰森*/


/*
let content = "叶影的诉说";
let key = "1234567890123456";
console.log("前端加密前：" + content);
console.log("前端加密密钥和解密密钥：" + key);
let encryptStr = encrypt(content, key);
console.log("前端加密后：" + encryptStr);
let decryptStr = decrypt(encryptStr, key);
console.log("前端解密后：" + decryptStr);

let encryptStrServer = "yMlouFhoG53UhHc93607gQ==";
console.log("后端原始密文：" + encryptStrServer);
let decryptStrServer = decrypt(encryptStrServer, key);
console.log("后端密文解密后：" + decryptStrServer);*/

